package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

func main() {
	weixinMap := make(map[string]Weixin)
	//file, _ := os.Open("D:/weixin.txt")
	file, _ := os.Open("D:/tblWeixinNew.txt")
	defer file.Close()
	reader := bufio.NewReader(file)
	for true {
		line, _, err := reader.ReadLine()
		if err == io.EOF {
			break
		}
		putMap(string(line), weixinMap)
	}
	printMap(weixinMap)
}

type Weixin struct {
	weixin_id string
	weixin_name string
	weixin_nick_name string
	weixin_avatar string
	sex int64
	phone int64
	create_time int64
	ext_data string
}

// 数据写入map
func putMap(line string, weixinMap map[string]Weixin) {
	line = line[0 : len(line) - 2]
	split := strings.Split(line, ", ")
	tmpMap := make(map[string]string)
	for idx, val := range split {
		if idx == 0 {
			//fmt.Println("str ==================", str)
			tmp := strings.Split(val, "=")
			tmpMap["weixin_id"] = tmp[1]
			continue
		}

		if !strings.Contains(val, "=") {
			continue
		}
		kv := strings.Split(val, "=")
		tmpMap[kv[0]] = kv[1]
	}
	// 拼接结构体
	weixin_id, _ := tmpMap["weixin_id"]
	weixin, ok := weixinMap[weixin_id]	//检查weixin_id是否已经在map中
	if !ok {	//不存在创建新结构体
		weixin = Weixin{weixin_id: weixin_id, weixin_name: "''",
			weixin_nick_name: "''", weixin_avatar: "''", sex: 0, phone: 0, create_time: 0, ext_data: "[]"}

	}
	weixin_name, ok := tmpMap["weixin_name"]
	if ok && len(weixin_name) > 2 && len(weixin.weixin_name) <= 2 {
		weixin.weixin_name = weixin_name
	}

	weixin_nick_name, ok := tmpMap["weixin_nick_name"]
	if ok && len(weixin_nick_name) > 2 && len(weixin.weixin_nick_name) <= 2 {
		weixin.weixin_nick_name = weixin_nick_name
	}
	weixin_avatar, ok := tmpMap["weixin_avatar"]
	if ("" == weixin.weixin_avatar || "''" == weixin.weixin_avatar) && ok {
		weixin.weixin_avatar = weixin_avatar
	}
	ext_data, ok := tmpMap["ext_data"]
	if ok && len(ext_data) > len(weixin.ext_data) {
		weixin.ext_data = ext_data
	}
	sex, ok := tmpMap["sex"]
	if 0 == weixin.sex && ok {
		weixin.sex, _ = strconv.ParseInt(sex, 0, 64)
	}
	phone, ok := tmpMap["phone"]
	if 0 == weixin.phone && ok {
		weixin.phone, _ = strconv.ParseInt(phone, 0, 64)
	}

	create_time, ok := tmpMap["create_time"]
	if ok {
		weixin.create_time, _ = strconv.ParseInt(create_time, 0, 64)
	}

	weixinMap[weixin_id] = weixin
	return
}

func toSql(weixin Weixin) string {
	sql := fmt.Sprintf("INSERT IGNORE tblWeixin SET weixin_id=%s, weixin_name=%s, weixin_nick_name=%s, weixin_avatar=%s, sex=%d, phone=%d, create_time=%d, ext_data=%s;",
		weixin.weixin_id, weixin.weixin_name, weixin.weixin_nick_name, weixin.weixin_avatar, weixin.sex, weixin.phone, weixin.create_time, weixin.ext_data)
	fmt.Println(sql)
	return sql
}

func printMap(weixinMap map[string]Weixin)  {
	filename := "D:/dest_sql.txt"
	file, _ := os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
	defer file.Close()
	cnt := 0
	for _, weixin := range weixinMap {
		file.WriteString(fmt.Sprintf("%s\n", toSql(weixin)))
		//file.WriteString(fmt.Sprintf("%s\n", weixin.weixin_id))
		cnt ++
	}

	fmt.Println("cnt is ", cnt)
}